package com.station.service.mapper;

import com.station.annotation.DataSource;
import com.station.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description:
 * @Author: dongfangyu
 * @Date: 2025/10/13
 */
@Mapper
public interface SaleOrderMapper {


    @DataSource("order")
    @Select(" select \n" +
            " so.orderNo,\n" +
            " so.orderStatus,\n" +
            " so.source,\n" +
            " so.deliveryStatus,\n" +
            " so.payFlag,\n" +
            " so.createDate,\n" +
            " so.orderStore,\n" +
            " so.realPayAmount,\n" +
            " so.discount,\n" +
            " so.specAmount,\n" +
            " so.couponAmount,\n" +
            " so.orderAmount,\n" +
            " so.modifyDate\n" +
            "  from saleorder so\n" +
            "where \n" +
            " so.vendorId = #{vendorId} \n" +
            " and so.df = 0\n" +
            " and so.createDate >= STR_TO_DATE(#{startDate},'%Y-%m-%d %H:%i:%s')\n" +
            " and so.createDate >= STR_TO_DATE(#{startDate},'%Y-%m-%d %H:%i:%s') \n" +
            " and so.orderFlag = 0 and so.source in (0,4)  and so.payFlag = 1\n" +
            " and so.orderStatus in (3,5,9,11)\n" +
            " UNION\n" +
            " select \n" +
            " so.orderNo,\n" +
            " so.orderStatus,\n" +
            " so.source,\n" +
            " so.deliveryStatus,\n" +
            " so.payFlag,\n" +
            " so.createDate,\n" +
            " so.orderStore,\n" +
            " so.realPayAmount,\n" +
            " so.discount,\n" +
            " so.specAmount,\n" +
            " so.couponAmount,\n" +
            " so.orderAmount,\n" +
            " so.modifyDate\n" +
            " from saleorder so\n" +
            "  where\n" +
            " so.vendorId = #{vendorId} \n" +
            " and so.df = 0\n" +
            " and so.createDate >= STR_TO_DATE(#{startDate},'%Y-%m-%d %H:%i:%s')\n" +
            " and so.createDate <= STR_TO_DATE(#{endDate},'%Y-%m-%d %H:%i:%s') \n" +
            " and so.orderFlag = 0 and so.source = 1 and so.payFlag = 1\n" +
            " and so.orderStatus = 3\n" +
            " and so.deliveryStatus = 4 ")
    List<SaleOrder> querySaleOrderList(@Param("vendorId")Long vendorId, @Param("startDate") String startDate, @Param("endDate") String endDate);

    @DataSource("order")
    @Select("select \n" +
            " so.orderNo,\n" +
            " so.orderStatus,\n" +
            " so.source,\n" +
            " so.deliveryStatus,\n" +
            " so.payFlag,\n" +
            " so.createDate,\n" +
            " so.orderStore,\n" +
            " so.realPayAmount,\n" +
            " so.discount,\n" +
            " so.specAmount,\n" +
            " so.couponAmount,\n" +
            " so.orderAmount,\n" +
            " so.modifyDate\n" +
            " from saleorder so\n" +
            "  where\n" +
            " so.vendorId = #{vendorId} \n" +
            " and so.df = 0\n" +
            " and so.createDate >= STR_TO_DATE(#{startDate},'%Y-%m-%d %H:%i:%s')\n" +
            " and so.createDate <= STR_TO_DATE(#{endDate},'%Y-%m-%d %H:%i:%s')\n" +
            " and so.orderFlag = 0 and so.source = 1 and so.payFlag = 1\n" +
            " and so.deliveryStatus != 4")
    List<SaleOrder> queryWaitVerifyOrderList(@Param("vendorId")Long vendorId,@Param("startDate")String startDate,@Param("endDate")String endDate);

    @DataSource("order")
    @Select("select \n" +
            " so.orderNo,\n" +
            " so.orderStatus,\n" +
            " so.source,\n" +
            " so.deliveryStatus,\n" +
            " so.payFlag,\n" +
            " so.createDate,\n" +
            " so.orderStore,\n" +
            " so.realPayAmount,\n" +
            " so.discount,\n" +
            " so.specAmount,\n" +
            " so.couponAmount,\n" +
            " so.orderAmount,\n" +
            " so.modifyDate\n" +
            "from \n" +
            "saleorder so\n" +
            "inner join saleorderlog sol on sol.orderNo = so.orderNo  and sol.opType = 4\n" +
            "where\n" +
            "so.df = 0\n" +
            "and so.deliverDate >= STR_TO_DATE(#{startDate},'%Y-%m-%d %H:%i:%s')\n" +
            "and so.deliverDate <= STR_TO_DATE(#{endDate},'%Y-%m-%d %H:%i:%s') ")
    List<SaleOrder> queryVerifyOrderList(@Param("startDate")String startDate,@Param("endDate")String endDate);

    @DataSource("order")
    @Select("select " +
            "orderNo,skuCode,skuName,barcode,salePlusCode,skuNum,salePrice,realPrice,amount,saleUnit,stockUnit,createDate,preAmount,discount " +
            "from saledetail " +
            "where orderNo = #{orderNo} ")
    List<OrderDetail> queryOrderDetailList(String orderNo);

    @DataSource("order")
    @Select("select \n" +
            "sr.orderNo,\n" +
            "sr.orderStore,\n" +
            "so.modifyDate,\n" +
            "sr.origNo\n" +
            "from saleorder so\n" +
            "inner join saleorder sr on sr.origNo = so.orderNo \n" +
            "where\n" +
            "so.df = 0\n" +
            "and so.modifyDate >= STR_TO_DATE(#{startDate},'%Y-%m-%d %H:%i:%s')\n" +
            "and so.modifyDate <= STR_TO_DATE(#{endDate},'%Y-%m-%d %H:%i:%s')\n" +
            "and so.orderStatus = 10 and so.orderFlag = 0 ")
    List<SaleOrder> queryRefundOrderList(@Param("startDate")String startDate,@Param("endDate")String endDate);

    @DataSource("order")
    @Select("select " +
            "orderNo,payCode,payAmount,realPayAmount,createDate,modifyDate " +
            "from salepay where df=0 and orderNo = #{orderNo}")
    List<SalePay> queryOrderPayWayList(String orderNo);

    @DataSource("erp")
    @Select("SELECT " +
            "orderNo, " +
            "status, " +
            "storeCode, " +
            "useQuantity, " +
            "useAmount, " +
            "bizDate, " +
            "approveDate, " +
            "purpose " +
            "FROM " +
            "useorder " +
            "WHERE df = 0 " +
            "AND vendorId = #{vendorId} " +
            "AND approveDate >= STR_TO_DATE(#{startDate},'%Y-%m-%d %H:%i:%s') " +
            "AND approveDate <= STR_TO_DATE(#{endDate},'%Y-%m-%d %H:%i:%s') " +
            "AND `status` = 2 ")
    List<UseOrder> queryUseOrderList(@Param("vendorId")Long vendorId,@Param("startDate")String startDate,@Param("endDate")String endDate);

    @DataSource("erp")
    @Select("SELECT " +
            "skuCode,stockUnit,salePrice,useQuantity,useAmount,realCostTotal,realCostPrice,costPrice " +
            "FROM usedetail " +
            "WHERE df = 0 " +
            "AND orderNo = #{orderNo}")
    List<UseDetail> queryUseOrderDetailList(String orderNo);

}
